#include "Input.h"
#include "Core.h"

//#include <xinput.h>

void Input_UpdateGUIState(float x, float y, int mb);

bool keysThisFrame[ALLEGRO_KEY_MAX];
bool keysLastFrame[ALLEGRO_KEY_MAX];

int mJoyToKey[32];

Vec JoystickStick[2] = { Vec(), Vec() };

bool WasPressed(int aKey)
{
	return (keysThisFrame[aKey] && !keysLastFrame[aKey]);
}

bool WasReleased(int aKey)
{
	return (!keysThisFrame[aKey] && keysLastFrame[aKey]);
}

bool IsPressed(int aKey)
{
	return keysThisFrame[aKey];
}

void InitInput()
{
	for(int k=0; k<ALLEGRO_KEY_MAX; k++)
	{
		keysLastFrame[k] = false;
		keysThisFrame[k] = false;
	}

	memset(mJoyToKey, 0, sizeof(mJoyToKey));

	mJoyToKey[1] = KEY_A;
	mJoyToKey[2] = KEY_B;
	mJoyToKey[3] = KEY_X;
	mJoyToKey[4] = KEY_Y;
	mJoyToKey[7] = KEY_R;
	mJoyToKey[6] = KEY_L;

	//XInputEnable(true);
}

void DoInput()
{
	for(int k=0; k<ALLEGRO_KEY_MAX; k++)
	{
		keysLastFrame[k] = keysThisFrame[k];
	}

	ALLEGRO_MOUSE_STATE st;
	al_get_mouse_state(&st);

	Input_UpdateGUIState(float(st.x)*ScreenScale, float(st.y)*ScreenScale, st.buttons);
}

void Input_HandleEvent(ALLEGRO_EVENT& aEvent)
{
	if(aEvent.type == ALLEGRO_EVENT_KEY_DOWN)
	{
		keysThisFrame[aEvent.keyboard.keycode] = true;
	}
	else if (aEvent.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN)
	{
		keysThisFrame[mJoyToKey[aEvent.joystick.button]] = true;
	}

	else if(aEvent.type == ALLEGRO_EVENT_KEY_UP)
	{
		keysThisFrame[aEvent.keyboard.keycode] = false;
	}
	else if (aEvent.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_UP)
	{
		keysThisFrame[mJoyToKey[aEvent.joystick.button]] = false;
	}

	else if (aEvent.type == ALLEGRO_EVENT_JOYSTICK_AXIS)
	{
		/// Movement
		//printf("Stick: %i, Axis: %i, Pos: %g\n", aEvent.joystick.stick, aEvent.joystick.axis, aEvent.joystick.pos);

		if (aEvent.joystick.stick == 0)
			(&JoystickStick[0].X)[aEvent.joystick.axis] = aEvent.joystick.pos;

		if (aEvent.joystick.stick == 2)
		{
			if (aEvent.joystick.axis == 1)
			{
				if (aEvent.joystick.pos > 0.1f)
					keysThisFrame[ALLEGRO_KEY_DOWN] = true;
				else if (aEvent.joystick.pos < -0.1f)
					keysThisFrame[ALLEGRO_KEY_UP] = true;
				else
				{
					keysThisFrame[ALLEGRO_KEY_DOWN] = false;
					keysThisFrame[ALLEGRO_KEY_UP] = false;
				}
			} else if (aEvent.joystick.axis == 0)
			{
				if (aEvent.joystick.pos > 0.1f)
					keysThisFrame[ALLEGRO_KEY_RIGHT] = true;
				else if (aEvent.joystick.pos < -0.1f)
					keysThisFrame[ALLEGRO_KEY_LEFT] = true;
				else
				{
					keysThisFrame[ALLEGRO_KEY_LEFT] = false;
					keysThisFrame[ALLEGRO_KEY_RIGHT] = false;
				}
			}
		} else if (aEvent.joystick.stick == 1)
		{
			if (aEvent.joystick.axis == 0)
			{
				if (aEvent.joystick.pos > 0.1f)
					keysThisFrame[ALLEGRO_KEY_DOWN] = true;
				else if (aEvent.joystick.pos < -0.1f)
					keysThisFrame[ALLEGRO_KEY_UP] = true;
				else
				{
					keysThisFrame[ALLEGRO_KEY_UP] = false;
					keysThisFrame[ALLEGRO_KEY_DOWN] = false;
				}
			}
		} else if (aEvent.joystick.stick == 0)
		{
			if (aEvent.joystick.axis == 2)
			{
				if (aEvent.joystick.pos > 0.1f)
					keysThisFrame[ALLEGRO_KEY_RIGHT] = true;
				else if (aEvent.joystick.pos < -0.1f)
					keysThisFrame[ALLEGRO_KEY_LEFT] = true;
				else
				{
					keysThisFrame[ALLEGRO_KEY_RIGHT] = false;
					keysThisFrame[ALLEGRO_KEY_LEFT] = false;
				}
			}
		}
	}

	else if (aEvent.type == ALLEGRO_EVENT_JOYSTICK_CONFIGURATION)
	{
		al_reconfigure_joysticks();
	}

	else if (aEvent.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
	{
		keysThisFrame[MOUSE_LEFT+(aEvent.mouse.button-1)] = true;
	}
	else if (aEvent.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)
	{
		keysThisFrame[MOUSE_LEFT+(aEvent.mouse.button-1)] = false;
	}
}

void SetVibration( int left, int right )
{
	/*
	XINPUT_VIBRATION vibration;
	ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION));

	vibration.wLeftMotorSpeed = left;
	vibration.wRightMotorSpeed = right;

	XInputSetState(0, &vibration);
	*/
}

Vec GetMousePos()
{
	ALLEGRO_MOUSE_STATE state;
	al_get_mouse_state(&state);
	return WindowToScreen(Vec(state.x, state.y));
}